package com.ctshk.common.utils;

import java.util.HashMap;
import java.util.Map;

/**
 * 单房差算法
 */
public class SingleRoomUtil {

    public static void main(String[] args) {
        computeRooms(3,3,3, 2, 2);
    }

    /**
     * @param choose_rooms 选择的房间数量（选择房间数时传，其他情况可忽略，只使用返参：rooms）
     * @param lostMen 剩余未计算房间占床人数
     * @param lostChild 剩余未分配房间不占床人数
     * @param men 每个房间占床人数
     * @param child 每个房间不占床人数
     */
    public static Map<String, Integer> computeRooms(double lostMen,double lostChild, int choose_rooms,
        double men, double child){

        //总房间数
        double rooms = 0;
        //补单房差人数
        double singleRoomNum = 0;

        //总成人数量
        double cache_men = lostMen;

        //剩余人数不为零时继续计算
        while(lostMen != 0 || lostChild != 0){

            if(lostMen > 0){

                //如果剩余的成人数大于每个房间占床人数
                if(lostMen >= men){
                    lostMen = lostMen - men;

                    if(lostChild > 0){
                        if(lostChild >= child){
                            lostChild = lostChild - child;
                        }else{
                            lostChild = 0;
                        }
                    }

                    rooms++;
                }else{

                    double change_child_to_men = men - lostMen;

                    lostMen = 0;
                    rooms++;

                    if(lostChild > 0){

                        if(lostChild >= change_child_to_men){
                            lostChild = lostChild - change_child_to_men;
                        }else{
                            lostChild = lostChild - (change_child_to_men - lostChild);
                        }

                        if(lostChild > 0){
                            if(lostChild >= child){
                                lostChild = lostChild - child;
                            }else{
                                lostChild = lostChild - (child - lostChild);
                            }
                        }

                    }
                }


            }

            if(lostMen == 0 && lostChild > 0){

                if(lostChild >= (men+child)){

//					System.out.println("剩余的不占床人数："+lost_child);
//					System.out.println("需占床人数："+Math.ceil((lost_child/(men+child))));
//					System.out.println("求余数："+(lost_child % (men+child)));

                    rooms += Math.ceil(lostChild/(men+child));

                    lostChild = 0;
                }else{

                    rooms ++;
                    lostChild = 0;

                }
            }


        }

        Map<String, Integer> result = new HashMap<>();

        System.out.println("最终所需房间数 = "+rooms);
        result.put("rooms", (int)rooms);

        //单房差 = 房间数 * 每个房间占床人数 - 成人数
        singleRoomNum = rooms * men - cache_men;
        if(singleRoomNum < 0){
            singleRoomNum = 0;
        }

        System.out.println("最终所需补单房差人数(默认最少所需房间) = "+singleRoomNum);

        singleRoomNum = choose_rooms * men - cache_men;
        if(singleRoomNum < 0){
            singleRoomNum = 0;
        }

        System.out.println("最终所需补单房差人数(按选择房间数计算) = "+singleRoomNum);
        result.put("singleRoomNum", (int)singleRoomNum);

        return result;

    }

}
